package vn.nt.healthcare.server.ws.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vn.nt.healthcare.server.ws.domain.UserSetting;
import vn.nt.healthcare.server.ws.model.CompanyInfo;
import vn.nt.healthcare.server.ws.model.LoginInfo;
import vn.nt.healthcare.server.ws.service.CompanyService;
import vn.nt.healthcare.server.ws.service.UserService;

@Controller("userController")
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private CompanyService companyService;

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public @ResponseBody
	LoginInfo login(HttpServletRequest request,
			@RequestParam(value = "u", required = true) String uuid,
			@RequestParam(value = "p", required = true) String password) {
		return userService.login(uuid, password);
	}

	@RequestMapping(value = "/loginbyuser", method = RequestMethod.GET)
	public @ResponseBody
	LoginInfo loginUser(HttpServletRequest request,
			@RequestParam(value = "u", required = true) String username,
			@RequestParam(value = "p", required = true) String password) {
		System.out.println("|===============================>>> " + username);
		return userService.loginUser(username, password);
	}

	@RequestMapping(value = "/company", method = RequestMethod.GET)
	public @ResponseBody
	List<CompanyInfo> getCompany(HttpServletRequest request,
			@RequestParam(value = "sid", required = true) String sid) {
		return userService.findCompanies(sid);
	}

	@RequestMapping(value = "/company/user", method = RequestMethod.GET)
	public @ResponseBody
	List<CompanyInfo> getCompanyByUser(HttpServletRequest request,
			@RequestParam(value = "u", required = true) String uuid,
			@RequestParam(value = "p", required = true) String password) {
		return userService.findCompanies(uuid, password);
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public @ResponseBody
	LoginInfo register(HttpServletRequest request, @RequestBody UserSetting user) {
		LoginInfo result = userService.register(user);
		return result;
	}

	@RequestMapping(value = "/register/push", method = RequestMethod.POST)
	public @ResponseBody
	LoginInfo register(HttpServletRequest request,
			@RequestBody UserSetting user,
			@RequestParam(value = "os", required = true) String os,
			@RequestParam(value = "token", required = true) String token,
			@RequestParam(value = "app", required = true) String appName) {
		LoginInfo result = userService.register(user);
		return result;
	}
}
